package com.pengweiwei.androidzygote.functions.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.pengweiwei.androidzygote.R;
import com.putong35.common.baseactfrag.CompState;
import com.putong35.common.baseactfrag.adapter.RecyclerViewBaseAdapter;
import com.putong35.common.baseactfrag.fragment.SwipeListFragment;
import com.putong35.common.httprequest.Response;
import com.putong35.common.utils.io.AssetsUtil;

import java.io.Serializable;
import java.util.List;


/**
 * 刷列表的模板
 * <p>
 * 主要分为三步：fragment  + adapter(+ layout布局)  + bean
 * <p>
 * 2. adapter要设置 泛型参数（LoginResponce）  e.g extends RecyclerViewBaseAdapter<LoginResponce>
 */
public class MyListFragment extends SwipeListFragment {

//    private final static String URL_TEST = "http://hpapi.baoruan.com:10081/test/list";
private final static String URL_TEST = "http://apitest.baoruan.com/launcher/default/index?version=9.150320&un=239BA6301606E596F70AC1E7B4808B23&name=recommendlist&channelId=ceshi_baoruanservice1";

    private int mPage = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        init();
        startLoading();
        return view;
    }

    private void init() {
        mPage = 0;
        clearDecorations();
        setPullToRefreshEnable(false);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

//    private ListDataRequestListener<TemplateBean> mResponceListener = new ListDataRequestListener<>(this, TemplateBean.class);

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        return new TemplateListAdapter(getActivity());
    }

    @Override
    public void onStartLoading() {
        String responseStr = AssetsUtil.getStringFromAssets(getActivity(), "list_ui_test_data.txt");
        Response parser = new Response(responseStr);
        List<TemplateBean> list = parser.getDataListBean(TemplateBean.class);
        if (list != null && list.size() > 0) {
            getAdapter().getData().addAll(list);
        }

//        setState(CompState.DATA);

        setState(CompState.DATA);
    }




    /**
     * Adapter
     */
    public class TemplateListAdapter extends RecyclerViewBaseAdapter<TemplateBean> {

        public TemplateListAdapter(Context con) {
            super(con);
        }

        @Override
        public RecyclerView.ViewHolder onCreateHolder(ViewGroup viewGroup, int i) {
            return new ItemViewHolder(setAdapterViewItem(viewGroup, R.layout.listitem_template));
        }


        @Override
        public void onBindHolder(RecyclerView.ViewHolder viewHolder, int i) {
            ItemViewHolder holder = (ItemViewHolder) viewHolder;
            final TemplateBean bean = getData().get(i);
//            ImageLoader.with(RequestDemoListActivity.this).load(bean.getIconUrl()).into(holder.ivIcon).start();
            holder.tvText.setText(bean.getName());
        }

        public class ItemViewHolder extends RecyclerView.ViewHolder {
            public RelativeLayout lytMain;
            public TextView tvText;
            public ImageView ivIcon;

            public ItemViewHolder(View itemView) {
                super(itemView);
                lytMain = (RelativeLayout) itemView.findViewById(R.id.lytMain);
                ivIcon = (ImageView) itemView.findViewById(R.id.ivIcon);
                tvText = (TextView) itemView.findViewById(R.id.tvText);
            }
        }
    }


    /**
     * 数据对象 Bean
     */
    public static class TemplateBean implements Serializable{

        /**
         * id : 333
         * price : 333.336
         * age : 36
         * iconUrl : http://pic.58pic.com/58pic/15/28/08/76X58PIC2UP_1024.jpg
         * name : 星辰
         * imageList : ["http://pic.58pic.com/58pic/15/28/08/76X58PIC2UP_1024.jpg","http://pic.58pic.com/58pic/15/28/08/76X58PIC2UP_1024.jpg","http://pic.58pic.com/58pic/15/28/08/76X58PIC2UP_1024.jpg"]
         */

        private int id;
        private double price;
        private int age;
        private String iconUrl;
        private String name;
        private List<String> imageList;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public double getPrice() {
            return price;
        }

        public void setPrice(double price) {
            this.price = price;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getIconUrl() {
            return iconUrl;
        }

        public void setIconUrl(String iconUrl) {
            this.iconUrl = iconUrl;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<String> getImageList() {
            return imageList;
        }

        public void setImageList(List<String> imageList) {
            this.imageList = imageList;
        }
    }

}
